#!/usr/bin/python3
import argparse
import os
import subprocess


def run_all_step(submission_path):

    scan_file_path = os.path.join(submission_path, 'src', 'scan_insertion')
    scan_cmd = [scan_file_path]
    print('start scan', flush=True)
    scan_process = subprocess.Popen(scan_cmd, stdout=subprocess.PIPE)
    scan_process.communicate()

    cat_file_path = os.path.join(submission_path, 'src', 'cat_gen')
    if os.path.exists(cat_file_path):
        cat_cmd = [cat_file_path]
        print('start cat gen', flush=True)
        cat_process = subprocess.Popen(cat_cmd, stdout=subprocess.PIPE)
        cat_process.communicate()
    else:
        print('skip cat gen', flush=True)

    atpg_file_path = os.path.join(submission_path, 'src', 'dc_atpg')
    atpg_cmd = [atpg_file_path]
    print('start atpg', flush=True)
    atpg_process = subprocess.Popen(atpg_cmd, stdout=subprocess.PIPE)
    atpg_process.communicate()

    print('run all step finished', flush=True)
    return 0

if __name__ == '__main__':
    parser = argparse.ArgumentParser(description='Process scan_insertion, cat_gen and dc_atpg')
    parser.add_argument('submission_path', type=str, help='作品提交目录')
    args = parser.parse_args()
    submission_path = args.submission_path
    run_all_step(submission_path)
